package org.gdstash.ui.select;

import org.gdstash.db.SelectionCriteria;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.text.AbstractDocument;
import javax.swing.text.DocumentFilter;
import java.awt.*;

public class LevelSelPane extends AdjustablePanel {
    private JLabel lblLevelMin;
    private JLabel lblLevelMax;
    private JFormattedTextField ftLevelMin;
    private JFormattedTextField ftLevelMax;

    public LevelSelPane() {
        this(0);
    }

    public LevelSelPane(final int direction) {
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        if (direction == 0) {
            hGroup.addGroup(layout.createParallelGroup().addComponent(this.lblLevelMin)).addGroup(layout.createParallelGroup().addComponent(this.ftLevelMin)).addGroup(layout.createParallelGroup().addComponent(this.lblLevelMax)).addGroup(layout.createParallelGroup().addComponent(this.ftLevelMax));
        } else {
            hGroup.addGroup(layout.createParallelGroup().addComponent(this.lblLevelMin).addComponent(this.lblLevelMax)).addGroup(layout.createParallelGroup().addComponent(this.ftLevelMin).addComponent(this.ftLevelMax));
        }
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        if (direction == 0) {
            vGroup.addGroup(layout.createParallelGroup().addComponent(this.lblLevelMin).addComponent(this.ftLevelMin).addComponent(this.lblLevelMax).addComponent(this.ftLevelMax));
        } else {
            vGroup.addGroup(layout.createParallelGroup().addComponent(this.lblLevelMin).addComponent(this.ftLevelMin)).addGroup(layout.createParallelGroup().addComponent(this.lblLevelMax).addComponent(this.ftLevelMax));
        }
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.lblLevelMin, this.ftLevelMin);
        layout.linkSize(0, this.lblLevelMin, this.lblLevelMax);
        layout.linkSize(0, this.lblLevelMin, this.ftLevelMax);
        layout.linkSize(1, this.lblLevelMin, this.ftLevelMin);
        layout.linkSize(1, this.lblLevelMin, this.lblLevelMax);
        layout.linkSize(1, this.lblLevelMin, this.ftLevelMax);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        Font fntFText = UIManager.getDefaults().getFont("FormattedTextField.font");
        if (fntFText == null) {
            fntFText = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntFText = fntFText.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_REQUIREMENTS"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntFText = fntFText.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        if (this.lblLevelMin == null) {
            this.lblLevelMin = new JLabel();
        }
        this.lblLevelMin.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_LEVEL_MIN"));
        this.lblLevelMin.setFont(fntLabel);
        if (this.ftLevelMin == null) {
            this.ftLevelMin = new JFormattedTextField();
            final DocumentFilter filter = new IntLenDocFilter(3);
            final AbstractDocument doc = (AbstractDocument) this.ftLevelMin.getDocument();
            doc.setDocumentFilter(filter);
        }
        this.ftLevelMin.setFont(fntFText);
        if (this.lblLevelMax == null) {
            this.lblLevelMax = new JLabel();
        }
        this.lblLevelMax.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_LEVEL_MAX"));
        this.lblLevelMax.setFont(fntLabel);
        if (this.ftLevelMax == null) {
            this.ftLevelMax = new JFormattedTextField();
            final DocumentFilter filter = new IntLenDocFilter(3);
            final AbstractDocument doc = (AbstractDocument) this.ftLevelMax.getDocument();
            doc.setDocumentFilter(filter);
        }
        this.ftLevelMax.setFont(fntFText);
    }

    public void clear() {
        this.ftLevelMin.setText(null);
        this.ftLevelMax.setText(null);
    }

    public int getMinLevel() {
        int i = -1;
        final String s = this.ftLevelMin.getText();
        try {
            i = Integer.parseInt(s);
        } catch (NumberFormatException ex) {
            i = -1;
        }
        return i;
    }

    public int getMaxLevel() {
        int i = -1;
        final String s = this.ftLevelMax.getText();
        try {
            i = Integer.parseInt(s);
        } catch (NumberFormatException ex) {
            i = -1;
        }
        return i;
    }

    public void addCriteria(final SelectionCriteria criteria) {
        final int minLevel = this.getMinLevel();
        final int maxLevel = this.getMaxLevel();
        if (minLevel != -1) {
            criteria.levelMin = minLevel;
        }
        if (maxLevel != -1) {
            criteria.levelMax = maxLevel;
        }
    }
}
